# -*- coding: utf-8 -*-
#   没有树结构的单表F7帮助
# @Time    ： 2023/5/22  13:16
# @Auther  ： 徐良
# File     :  f7HelpPanelSimple.py
import re
from PySide6.QtCore import Signal, Qt
from PySide6.QtWidgets import QMainWindow, QApplication
from PySide6.QtGui import QAction
from mesParts.components.ui.f7HelpPanelSimpleUI import Ui_MainWindow
# from share.dataFactory import DataFactory
# from share.funPublic import getSWO
# from share.dataFactory import BIO_DB


class F7HelpPanelSimple(QMainWindow, Ui_MainWindow):
    F7PanelRetSignal = Signal(dict)  # F7帮助面板发出的返回信号，dict = self.retMap

    def __init__(self, paraMap, parent=None):
        self.helpModel = None
        super(F7HelpPanelSimple, self).__init__(parent)
        self.setAttribute(Qt.WA_StyledBackground, True)
        self.setupUi(self)
        self.initState = 0
        self.paraMap = paraMap  # 加这条代码可以省略self.obj = paraMap['obj']
        self.retMap = {}  # 返回值字典

        # self.tableWork = self.paraMap['tableWork']
        # self.f7dict = self.paraMap['f7dict']
        # self.tableFName = self.f7dict['tableFName']
        # self.searchSql = self.f7dict['searchSql']
        #
        # self.statusBar().showMessage('就绪')
        #
        # self.obj = paraMap['obj']
        #
        self.toolBar.actionTriggered[QAction].connect(self.processtrigger)
        self.setWindowModality(Qt.ApplicationModal)  # 设置为模态
        #
        # cboNameList = self.f7dict['comboNameList']
        # cboFieldList = self.f7dict['comboFieldList']
        #
        # for name, field in zip(cboNameList, cboFieldList):
        #     self.nameCbo.addItem(name, field)
        #
        # self.readHelpData()
        # self.setInfo()
        #
        # self.tableView.doubleClicked.connect(self.onTableViewDbClicked)

    def readHelpData(self):

        self.helpModel = DataFactory.getQueryModelForSql(self.searchSql)
        self.tableView.setModel(self.helpModel)
        heads = self.f7dict['headName']
        headList = heads.split(',')
        widths = self.f7dict['headWidth']
        widthList = widths.split(',')
        self.tableView.setHead(headList)
        self.tableView.setColumnWidthForList(widthList)

    def setInfo(self):
        self.setWindowTitle(self.f7dict['windowTitle'])
        self.lbl_infoitem.setText('{name},  共计{count}条记录'.
                                  format(name=self.f7dict['labelInfo'],
                                         count=self.helpModel.rowCount()))

    # 帮助表双击事件
    def onTableViewDbClicked(self, idx):
        self.makeRet(idx.row())

    def makeRet(self, row):
        retValueList = []

        getFieldList = self.f7dict['getFieldList']
        for f in getFieldList:
            v = self.helpModel.record(row).value(f)
            retValueList.append(v)

        retMap = {'retFieldList': self.f7dict['retFieldList'],
                       'retValueList': retValueList, 'fieldTypeList': self.f7dict['fieldTypeList'],
                  'obj': self.obj}
                       # 'panelStyle': self.panelStyle, }

        self.F7PanelRetSignal.emit(retMap)
        self.close()

    def processtrigger(self, q):

        if q.text() == '刷新':
            self.readHelpData()

        elif q.text() == '关闭':
            self.close()

    @Signal
    def on_cleanBtn_clicked(self):
        self.searchEdit.setText('')
        # self.readHelpData()


    @Signal
    def on_searchBtn_clicked(self):
        actual_value = self.nameCbo.currentActualValue()
        display_value = self.nameCbo.currentDisplayValue()

        scondi = self.searchEdit.text().strip()
        swo = getSWO(self.searchSql)
        s = swo['s']
        w = swo['w']
        o = swo['o']

        # 字段之前要考虑是否需要加别名
        r = re.search(r'a\.\w{2,}', s)
        condi = " {fieldName} like '%" + scondi + "%'"

        if r is not None:  # 有别名
            actual_value = 'a.' + actual_value
        condi = condi.format(fieldName=actual_value)

        # 改变 where 条件
        if w == '':  # 原始的SQL语句中没有where 条件
            w = "where" + condi
        else:
            w = w + " and " + condi
        sql = swo['s'] + ' ' + w + ' ' + o

        self.helpModel.setQuery(sql, BIO_DB)


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    dic = {}
    window = F7HelpPanelSimple(dic)
    window.show()
    sys.exit(app.exec())
